package com.leetcode.August;

import javax.swing.tree.TreeNode;

/**
 * @Description: 112. 路径总和
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-25 10:26
 */
public class LC112路径总和 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        int sum=0;
        return dfs(root,targetSum,sum);
    }

    private boolean dfs(TreeNode root, int targetSum, int sum) {
        if(root==null){
            return false;
        }
        sum+=root.val;
        if(root.left==null&&root.right==null){
            return sum==targetSum;
        }
        return dfs(root.left,targetSum,sum)||dfs(root.right,targetSum,sum);
    }
}
